﻿#include <QCoreApplication>
#include <QDebug>
#include <QTimer>
#include <QElapsedTimer>
#include <QThread>
#ifdef Q_CC_MSVC
#pragma execution_character_set("utf-8")
#endif
#include "imagewriterworker.h"
#include "framedata.h"

//最简单的就是利用Qt的信号和槽机制进行图像保存，当前测试场景为一秒1000张照片
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qRegisterMetaType<FrameData>("FrameData");

    QThread *thread = new QThread;
    ImageWriterWorker *worker = new ImageWriterWorker;

    worker->moveToThread(thread);
    thread->start();

    QElapsedTimer ela;
    ela.start();
    QByteArray rawData(640 * 512 * 2, Qt::Uninitialized);
    memset(rawData.data(), 100 % 256, rawData.size());
    for (int i = 0; i < 5000; ++i) {
        QThread::msleep(1);
        FrameData frame;
        frame.frameIndex = i;
        frame.rawData = rawData;

        QMetaObject::invokeMethod(worker, "writeFrame",
                                  Qt::QueuedConnection,
                                  Q_ARG(FrameData, frame));
        if(i%100==0)
        {
            qDebug() << __FUNCTION__ << __LINE__ <<i;
        }
    }

    qDebug() << __FUNCTION__ << __LINE__ << "所有帧已发送，准备退出 " <<ela.elapsed()<<"ms";

    // 安全退出流程
    QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater);
    QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);

    return a.exec();
}
